import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { FormBuilder, Validators } from '@angular/forms';
import { ApiService } from '../../../core/api/api.service';
import { MessageService } from '../../../core/service/message.service';
import { NzMessageService, NzModalService } from 'ng-zorro-antd';
import * as FileSaver from 'file-saver';
import * as XLSX from 'xlsx';
/**
 * 未修复漏洞
 */

@Component({
    templateUrl: './unRepairBugs.html',
})
export class UnRepairBugsComponent implements OnInit {
    pages = {
        pageNo: 1,
        pageSize: 5,
        total: 5,
        name: '',
        status: 'NotFixed' // 修复状态 Fixed 已修复, NotFixed 未修复
    };
    selectRow: any = {};
    dataList = [];
    tableLoading = false;
    ngOnInit() {
        this.loadList()
    }
    constructor(
        private router: Router,
        private fb: FormBuilder,
        private apiService: ApiService,
        private message: MessageService,
        private nzMessageService: NzMessageService,
        private modal: NzModalService
    ) { }
    selectThis(data) {
        this.selectRow = data;
    }
     
    getToCsv(){
        const exportItem = this.dataList; //this.item是一个数组，包含需要导出的内容
        const worksheet:XLSX.WorkSheet = XLSX.utils.json_to_sheet(exportItem);
        const workbook = { Sheets: { 'data': worksheet }, SheetNames: ['data'] };
        const excelBuffer = XLSX.write(workbook, { bookType: 'xlsx', type: 'array' });
        this.saveAsExcelFile(excelBuffer, 'report');
    }
    private saveAsExcelFile(buffer: any, fileName: string) {
        const data: Blob = new Blob([buffer], {
          type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8'
        });
        FileSaver.saveAs(data, fileName + '.xlsx');
      }
    search() {
        this.loadList();
    }
    loadList() {
        this.apiService.post('漏洞列表', this.pages).then((res: any) => {
            this.dataList = res.data.list;
            this.pages.total = res.data.pages;
            this.pages.pageNo = res.data.pageNo;
        })
    }
}
